package Server.Agent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

public class GameConfig{	
	static final boolean SSL = System.getProperty("ssl") != null;
	static final int PORT = Integer.parseInt(System.getProperty("port", SSL ? "8443" : "9300"));
	
	Properties m_pp = new Properties();  
	
	private static class SingletonHolder {
		private static final GameConfig INSTANCE = new GameConfig();
	}
	public static final GameConfig getInstance() {
		return SingletonHolder.INSTANCE;
	}		
	public GameConfig() {
		try
		{
	        String configpath = "./server.ini";  
		    
		    FileInputStream fis = new FileInputStream(configpath);  
		    m_pp.load(fis);    
	    		    
		    get("TCP_PORT");
		    get("APP_HOST");
		    
		    get("IP");
		    get("PORT");
		    get("USER");
		    get("PASS");
		    get("NAME");
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public String get(String key){		
	    try
	    {
	    	if(m_pp.get(key) != null)
	    	{
		    	String value = m_pp.get(key).toString();
		    	System.out.println( key + " : " + value);
			    return value;	
	    	}else{
	    		System.out.println( key + " : unknown");
	    	}
		}catch(Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}
	public int optInt(String key){		
	    try
	    {
	    	if(m_pp.get(key) != null)
	    	{
		    	String value = m_pp.get(key).toString();
		    	System.out.println( key + " : " + value);
		    	
			    return Integer.parseInt(value);	
	    	}else{
	    		System.out.println( key + " : unknown");
	    	}
		}catch(Exception e)
		{
			e.printStackTrace();
		}

		return 0;
	}
	public static boolean IsAnKongUid(int uid){
		return AnKongUids.indexOf(","+uid+",") != -1;
	}
	public static boolean IsSSZAnKongUid(int uid){
		return sszAnKongUids.indexOf(","+uid+",") != -1;
	}
	public static String AnKongUids = "";
	public static int AnKongPoint = 100;
	
	public static String sszAnKongUids = "";
	public static int sszAnKongPoint = 100;
	
	public static List<Integer> FengHaoList = new ArrayList<>();
	public static int SendGongGao() {
		try {
			String GongGaoStr = "";
			String encoding = "UTF8";
			File file = new File("./gonggao.txt");
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					GongGaoStr += lineTxt;
					GongGaoStr += "\r\n";
				}
				read.close();
				//dthall.getInstance().SendGongGao(GongGaoStr);
				File oldFile = file;
				if (!oldFile.exists()) {
					return 0;
				}
				String rootPath = oldFile.getParent();
				File newFile = new File(rootPath + File.separator + "gonggao.txt.end");
				if (oldFile.renameTo(newFile)) {
					System.out.println("修改成功!");
				} else {
					System.out.println("修改失败");
				}
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return 0;
	}
	public int getShuiShou(){
		return GameConfig.getInstance().optInt("SHUI_SHOU");
		
	
	}
	public  int shuishou() {
		String configpath = "./server.ini";  
	    
	    FileInputStream fis;
		try {
			fis = new FileInputStream(configpath);
		    m_pp.load(fis);   
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		 try
		    {
		    	if(m_pp.get("SHUI_SHOU") != null)
		    	{
			    	String value = m_pp.get("SHUI_SHOU").toString();
			    	
				    return Integer.parseInt(value);	
		    	}else{
		    	}
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		 return -1;
	}
	public static boolean AddFengHaoUid(int lUid) {
		if (FengHaoList.contains(lUid)) {
			return false;
		}
		FengHaoList.add(lUid);
		return true;
	}
	public static boolean IsFengHao(int lUid) {
		if (FengHaoList.contains(lUid)) {
			return true;
		}
		return false;
		
	}
	public static void RemveFengHaoUid(Integer uid) {
		// TODO Auto-generated method stub
		FengHaoList.remove(uid);
	}
}